#include "stdafx.h"
#include "LayoutHorizontal.h"
#include "Cell.h"
#include "FixCell.h"

LayoutHorizontal::LayoutHorizontal()
	: Layout()
	, m_fixWidth(0)
{
}


LayoutHorizontal::~LayoutHorizontal()
{
}

bool LayoutHorizontal::addColumn(unsigned int xInHundred)
{
	m_deno += xInHundred;
	m_cells.push_back(new Cell(xInHundred));
	return true;
}

bool LayoutHorizontal::addFixColumn(unsigned int width)
{
	m_cells.push_back(new FixCell(width, 0));

	m_fixWidth += width;

	return true;
}


void LayoutHorizontal::updateLayout()
{
	if (m_height == 0)
	{
		return;
	}

	// find fix cell

	int nNextX = 0;
	for (std::list<Cell*>::iterator iter = m_cells.begin(); iter != m_cells.end(); ++iter)
	{
		int width = 0;

		if ((*iter)->isFixSize())
		{
			width = (*iter)->Width();
		} 
		else
		{
			if (m_width < m_fixWidth)
			{
				width = 0;
			}
			else
			{
				int per = (*iter)->GetxInHundred();
				if (per == 0)
				{
					SK_ASSERT("fixed cell");
				}
				width = per * (m_width - m_fixWidth) / m_deno;
			}
		}

		if (nNextX >= m_width)
		{ // fix cell size bigger then total size
			(*iter)->setX(m_width);
		}
		else
		{
			(*iter)->setX(nNextX + m_x);
		}

		(*iter)->setY(m_y);
		(*iter)->setWidth(width);
		(*iter)->setHeight(m_height);

		(*iter)->updateLayout();

		nNextX += width;
	}
}